package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.listener.ExcelRespProp;
import com.TeacherInfoServer.pojo.DTO.LearningExperienceDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.LearningExperienceSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateLearningExperienceDTO;
import com.TeacherInfoServer.pojo.entity.LearningExperience;
import com.TeacherInfoServer.exception.RecordExistDetailException;
import com.TeacherInfoServer.mapper.LearningExperienceMapper;
import com.TeacherInfoServer.pojo.vo.LearningExperienceListVo;
import com.TeacherInfoServer.common.service.LearningExperienceService;
import com.TeacherInfoServer.common.service.TypeInfoService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LearningExperienceServiceImpl extends ServiceImpl<LearningExperienceMapper, LearningExperience>implements LearningExperienceService {
    @Autowired
    public LearningExperienceMapper learningExperienceMapper;
    @Autowired
    public TypeInfoService typeInfoService;
    @Override
    public Map<String, Object> getList(Integer pages, Integer size){
        Page<LearningExperienceListVo> page = new Page<>(pages, size);
        QueryWrapper<LearningExperience> queryWrapper = new QueryWrapper<>();
        learningExperienceMapper.getLearningExperienceByPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
    @Override
    public Map<String,Object> searchLearningExperienceList(LearningExperienceSearchDTO learningExperienceSearchDTO) {
        Page<LearningExperienceListVo> page = new Page<>(learningExperienceSearchDTO.getPages(), learningExperienceSearchDTO.getSize());
        learningExperienceMapper.searchLearningExperienceList(page, learningExperienceSearchDTO);
        Map<String, Object> data = new HashMap<>();
        data.put("totals", page.getTotal());
        data.put("currentPages", page.getCurrent());
        data.put("pageNumber", page.getPages());
        data.put("rows", page.getRecords());
        return data;
    }

    @Transactional
    @Override
    public void addLearningExperience(LearningExperienceDTO learningExperienceDTO){
        LearningExperience learningExperience=new LearningExperience();
        QueryWrapper<LearningExperience> queryWrapper = new QueryWrapper<>();
        if(learningExperienceMapper.selectOne(queryWrapper.eq("teacher_id",learningExperienceDTO.getTeacherId())
                .eq("degree",typeInfoService.getOneId("学位类型",learningExperienceDTO.getDegreeValue()))
                .eq("graduation_school",learningExperienceDTO.getGraduationSchool())
                .eq("major",learningExperienceDTO.getMajor())
                .eq("graduation_date",learningExperienceDTO.getGraduationDate())) == null) {
            learningExperience.setTeacherId(learningExperienceDTO.getTeacherId());
            learningExperience.setDegree(typeInfoService.getOneId("学位类型",learningExperienceDTO.getDegreeValue()));
            learningExperience.setGraduationSchool(learningExperienceDTO.getGraduationSchool());
            learningExperience.setMajor(learningExperienceDTO.getMajor());
            learningExperience.setGraduationDate(learningExperienceDTO.getGraduationDate());
            learningExperienceMapper.insert(learningExperience);
        }
        else {
            throw new RecordExistDetailException("已有完全相同记录");
        }
    }

    @Transactional
    @Override
    public void updateLearningExperience(UpdateLearningExperienceDTO learningExperience){
        QueryWrapper<LearningExperience> queryWrapper = new QueryWrapper<>();
        if(learningExperienceMapper.selectList(queryWrapper.eq("id",learningExperience.getId())
                .eq("teacher_id",learningExperience.getTeacherId())
                .eq("degree",typeInfoService.getOneId("学位类型",learningExperience.getDegree()))
                .eq("graduation_school",learningExperience.getGraduationSchool())
                .eq("major",learningExperience.getMajor())
                .eq("graduation_date",learningExperience.getGraduationDate())).size() ==1) {

            throw new RecordExistDetailException("未进行更新");
        }
        QueryWrapper<LearningExperience> queryWrapper1 = new QueryWrapper<>();
        if(!learningExperienceMapper.selectList(queryWrapper1
                .eq("teacher_id",learningExperience.getTeacherId())
                .eq("degree",typeInfoService.getOneId("学位类型",learningExperience.getDegree()))
                .eq("graduation_school",learningExperience.getGraduationSchool())
                .eq("major",learningExperience.getMajor())
                .eq("graduation_date",learningExperience.getGraduationDate())).isEmpty()) {

            throw new RecordExistDetailException("已有完全相同的记录");
        }
        UpdateWrapper<LearningExperience> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", learningExperience.getId())
                .set("teacher_id", learningExperience.getTeacherId())
                .set("degree", typeInfoService.getOneId("学位类型",learningExperience.getDegree()))
                .set("graduation_school", learningExperience.getGraduationSchool())
                .set("major", learningExperience.getMajor())
                .set("graduation_date", learningExperience.getGraduationDate());
        learningExperienceMapper.update(null,updateWrapper);


    }

    @Transactional
    @Override
    public boolean deleteLearningExperience(List<Long> ids){
        if (ids == null || ids.isEmpty()) {
            return false;
        }

        QueryWrapper<LearningExperience> queryWrapper= new QueryWrapper<>();
        queryWrapper.in("id",ids);
        int deleteCount=learningExperienceMapper.delete(queryWrapper);
        return deleteCount>0;
    }

    @Override
    @SneakyThrows(IOException.class)
    public void exportInfo(HttpServletResponse response){
        ExcelRespProp.setExcelRespProp(response, "教师学习经历信息列表");
        List<LearningExperienceListVo> learningExperiences = learningExperienceMapper.getLearningExperienceList();
        EasyExcel.write(response.getOutputStream())
                .head(LearningExperienceListVo.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("教师学习经历信息列表")
                .doWrite(learningExperiences);

    }

}
